{template 'common/header'}
{php echo $this -> set_tabbar($action, 4);}
{if $operation == 'display'}
<div class="main">
    <form action="" method="post" class="form-horizontal form">
        <div class="alert alert-warning">
            优惠券：{$coupon_count1}张　　代金券：{$coupon_count2}张　　礼品券：{$coupon_count3}张
        </div>
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row-fluid">
                    <div class="span8 control-group">
                        <a class="btn btn-primary" href="{php echo $this->createWebUrl('coupon', array('op' => 'post'))}"><i class="fa fa-plus"></i> 添加优惠券</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display'))}">全部</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display', 'attrtype' => 1))}">普通券({$type_count1})</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display', 'attrtype' => 2))}">营销券({$type_count2})</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="table-responsive panel-body">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width:10%;">显示顺序</th>
                        <th style="width:32%;">标题</th>
                        <th style="width:10%;">优惠券类型</th>
                        <th style="width:10%;">优惠券属性</th>
                        <th style="width:8%;">状态</th>
                        <th style="width:25%;">时间</th>
                        <th style="width:15%;">记录/编辑/删除</th>
                    </tr>
                    </thead>
                    <tbody id="level-list">
                    {loop $coupons $coupon}
                    <tr>
                        <td><input type="text" class="form-control" name="displayorder[{$coupon['id']}]"
                                   value="{$coupon['displayorder']}"></td>
                        <td>{$coupon['title']}</td>
                        <td>
                            {$coupon_type[$coupon['type']]}
                        </td>
                        <td>
                            {$coupon_attr_type[$coupon['attr_type']]}
                        </td>
                        <td>
                            {if TIMESTAMP<$coupon['starttime']}
                            <span class="label label-danger">未开始</span>
                            {elseif TIMESTAMP>$coupon['starttime'] && TIMESTAMP<$coupon['endtime']}
                            <span class="label label-success">进行中</span>
                            {else}
                            <span class="label label-danger">已结束</span>
                            {/if}
                        </td>
                        <td>
                            生效时间：{php echo date('Y-m-d H:i:s', $coupon[starttime]);}<br/>
                            过期时间：{php echo date('Y-m-d H:i:s', $coupon[endtime]);}
                        </td>
                        <td>
                            <a class="btn btn-default btn-sm"
                               href="{php echo $this->createWebUrl('sncodelist', array('op' => 'display', 'type' => $coupon['type'], 'id' => $coupon['id']))}"><i class="fa fa-bar-chart"></i></a>
                            <a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('coupon', array('op' => 'post', 'id' => $coupon['id']))}"><i class="fa fa-pencil"></i></a>
                            <a class="btn btn-default btn-sm" onclick="return confirm('确认删除吗？');return false;"
                               href="{php echo $this->createWebUrl('coupon', array('op' => 'delete', 'id' => $coupon['id']))}"><i class="fa fa-times"></i></a>
                        </td>
                    </tr>
                    {/loop}
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="7">
                            <input name="submit" type="submit" class="btn btn-primary" value="批量排序">
                            <input type="hidden" name="token" value="{$_W['token']}"/>
                        </td>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </form>
    {$pager}
</div>
{else if $operation == 'post'}
<div class="main">
    <form action="" method="post" onsubmit="return check();" class="form-horizontal form" enctype="multipart/form-data">
        <input type="hidden" id="id" name="id" value="{$reply['id']}"/>
        <div class="panel panel-default">
            <div class="panel-heading">
                {$title}
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券名称</label>
                    <div class="col-sm-9">
                        <input type="text" name="title" value="{$reply['title']}" id="title" class="form-control" placeholder="请输入优惠券名称"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券属性</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="attr_type" value="1" {if empty($reply)}checked="checked"{elseif $reply['attr_type']==1}checked="checked"{/if} />普通券
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="attr_type" value="2" {if $reply['attr_type']==2}checked="checked"{/if} />营销券(用于积分兑换)
                        </label>
                        <!--<label class="radio inline">-->
                        <!--<input type="radio" name="attr_type" value="3" {if $reply['attr_type']==3}checked="checked"{/if} />礼品券-->
                        <!--</label>-->
                        <div class="help-block" style="color:#f00;">选择营销券可以用于积分兑换.</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">开始时间</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_date('starttime', $starttime, true)}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">结束时间</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_date('endtime', $endtime, true)}
                    </div>
                </div>

                <div class="form-group attr_type2" id="level" style="{if $reply['attr_type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">选择人群</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="levelid" value="0" {if empty($reply)}checked="checked"{elseif $reply['levelid']==0}checked="checked"{/if} />所有会员
                        </label>
                        {loop $levels $level}
                        <label class="radio-inline">
                            <input type="radio" name="levelid" value="{$level['id']}" {if $reply['levelid']==$level['id']}checked="checked"{/if} >{$level['levelname']}
                        </label>
                        {/loop}
                        <script type="text/javascript">
                            $(function () {
                                $("input[name='levelid']").click(function () {
                                    var $ckone = $("#ckone"), cktwo = $("#cktwo");
                                    if ($(this).hasClass("dmoney")) {
                                        $ckone.show(); cktwo.hide();
                                    }
                                    else if ($(this).hasClass("mmoney")) {
                                        $ckone.hide(); cktwo.show();
                                    } else {
                                        $ckone.hide(); cktwo.hide();
                                    };
                                });
                            })
                        </script>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
                    <div class="col-sm-9">
                        <select class="form-control" name="store" id="store">
                            <option value="0">所有门店</option>
                            {loop $stores $item}
                            <option value="{$item['id']}" {if $item['id'] == $reply['storeid']}selected{/if}>{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券类型</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="type" value="1" {if empty($reply)}checked="checked"{elseif $reply['type']==1}checked="checked"{/if} />优惠券
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="type" value="2" {if $reply['type']==2}checked="checked"{/if} />代金券
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="type" value="3" {if $reply['type']==3}checked="checked"{/if} />礼品券
                        </label>
                    </div>
                </div>
                <div class="form-group type2" style="{if empty($reply) || $reply['type']!=2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">消费金额满</label>
                    <div class="col-sm-9">
                        <input type="text" name="give_value" value="{if empty($reply['give_value'])}0.00{else}{$reply['give_value']}{/if}" id="give_value" class="form-control" />
                    </div>
                </div>
                <div class="form-group type2" style="{if empty($reply) || $reply['type']!=2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">抵用金额</label>
                    <div class="col-sm-9">
                        <input type="text" name="dmoney" value="{if empty($reply['dmoney'])}0.00{else}{$reply['dmoney']}{/if}" id="dmoney" class="form-control" />
                    </div>
                </div>
                <div class="form-group attr_type2" style="{if $reply['attr_type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">发送总数</label>
                    <div class="col-sm-9">
                        <input type="text" name="totalcount" value="{if empty($reply['totalcount'])}0{else}{$reply['totalcount']}{/if}" id="totalcount" class="form-control" />
                        <div class="help-block" style="color:#f00;">0表示不限</div>
                    </div>
                </div>
                <div class="form-group attr_type2" style="{if $reply['attr_type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">每个用户可领取数</label>
                    <div class="col-sm-9">
                        <input type="text" name="count" value="{if empty($reply['count'])}0{else}{$reply['count']}{/if}" id="count" class="form-control" />
                        <div class="help-block" style="color:#f00;">0表示不限</div>
                    </div>
                </div>
                <div class="form-group type" style="{if $reply['type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券图片</label>
                    <div class="col-sm-9">
                        {if empty($thumb)}
                            {php echo tpl_form_field_image('thumb', '../addons/weisrc_icard/template/mobile/style1/images/coupon.jpg')}
                        {else}
                            {php echo tpl_form_field_image('thumb', $reply['thumb'])}
                        {/if}
                        <div class="help-block">大图片建议尺寸：720像素 * 400像素</div>
                    </div>
                </div>
                <div class="form-group type" style="{if $reply['type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">使用说明</label>
                    <div class="col-sm-9">
                        <textarea style="height:200px;" class="richtext form-control" name="content" id="content">{$reply['content']}</textarea>
                        <div class="help-block">在此说明券的使用方式，如最低消费金额，优惠券打折信息，不与其他优惠同时使用、节假日不可使用等。</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>
                    <div class="col-sm-9">
                        <input type="text" name="displayorder" value="{if empty($reply)}0{else}{$reply['displayorder']}{/if}" id="displayorder" class="form-control" />
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <input name="submit" type="submit" value="保存设置" class="btn btn-primary span3">
                <input type="hidden" name="token" value="{$_W['token']}"/>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        var property = ""
        $(":radio[name='attr_type']").click(function () {
            var $this = $(this);
            if(2 == $(this).val()){
                $(".attr_type2").hide();
            } else {
                $(".attr_type2").show();
            }
        });

        $(":radio[name='type']").click(function () {
            var $this = $(this);
            if(2 == $(this).val()){
                $(".type2").show();
                $(".type").hide();
            } else {
                $(".type2").hide();
                $(".type").show();
            }
        });
    });

    function check() {
        if($.trim($('#title').val()) == '') {
            message('没有输入标题.', '', 'error');
            return false;
        }
        if($.trim($('#title').val()).length > 30) {
            message('标题不能多于30个字.', '', 'error');
            return false;
        }
        var count = $.trim($('#count').val());
        if(count == '') {
            message('没有输入优惠券张数.', '', 'error');
            return false;
        }
        if(isNaN(count)){
            message('优惠券张数必须为数字.', '', 'error');
            return false;
        }
        return true;
    }
</script>
<script type="text/javascript">
    require(['jquery', 'util'], function ($, u) {
        $(function () {
            u.editor($('.richtext')[0]);
        });
    });
</script>
{/if}
{template 'common/footer'}
